package com.basic.app.controller;

import com.basic.app.config.security.JwtTokenUtils;
import com.basic.app.dto.AuthUserDto;
import com.basic.app.entity.member.LoginUser;
import com.basic.app.service.security.AuthService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * api登录授权
 */
@Slf4j
@RestController
@RequestMapping("/api/auth")
public class AuthController {
    @Resource
    private JwtTokenUtils jwtTokenUtils;
    private final AuthService authService;

    public AuthController(AuthService authService) {
        this.authService = authService;
    }

    @PostMapping(value = "/login")
    public ResponseEntity login(@RequestBody AuthUserDto authUserDto, HttpServletRequest request) {
        return ResponseEntity.ok(authService.login(authUserDto, request));
    }

    @GetMapping(value = "/a")
    public Object principal(String token){
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        LoginUser user = (LoginUser) principal;
        return jwtTokenUtils.getAuthentication(token).getPrincipal();
    }
}
